package com.hhsjdesign.dynamicui.utils

import android.content.Context
import android.view.View
import android.view.ViewGroup
import com.hhsjdesign.dynamicui.interfaces.IEnterable
import com.hhsjdesign.dynamicui.businessdata.IViewable
import com.hhsjdesign.dynamicui.core.property.viewdata.ItemViewData

/**
 * 创建视图和绑定视图的对象
 */
abstract class BusinessViewCreator<T>(val itemWidgetType: String) :
    IEnterable<T> {

    //这个方法可以在实现类复写，以返回xml对象,优先执行viewData创建视图，
    // 如果viewData为空，将必须要执行xml创建
    open fun createView(
        context: Context,
        itemWidgetType: String?,
        businessType: String?,
        itemViewData: ItemViewData?,
        parent: ViewGroup
    ): View {
        itemViewData?.let {
            return createTreeByViewData(context, itemViewData)
        }
        return createViewXml(context, itemWidgetType, businessType, parent) ?: View(context)
    }

    //创建View
    private fun createTreeByViewData(context: Context, itemWidget: ItemViewData): View {
        return itemWidget.createView(context)
    }

    //创建xml的布局
    open fun createViewXml(
        context: Context,
        itemWidgetType: String?,
        businessType: String?,
        parent: ViewGroup
    ): View? {
        return null
    }


    //绑定数据和事件
    open fun bindDataAndEvent(
        view: View,
        viewable: IViewable<*>?,
        itemWidgetType: String?,
        businessType: String?,
        itemWidget: ItemViewData?
    ) {
    }

    //绑定数据和事件
    open fun bindDataWithoutEvent(
        view: View,
        viewable: IViewable<*>?,
        itemWidgetType: String?,
        businessType: String?,
        itemWidget: ItemViewData?
    ) {
    }


}